// pages/shopManager/goodsManager/classifyEdit/classifyEdit.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        editClassifyShow: false,
        editClassifyName: '',
        editClassifyId: '',
        classifyList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getClassifyList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {
        let pages = getCurrentPages(); //获取当前页面实例
        let prepage = pages[pages.length - 2]
        if (prepage == undefined || prepage == null) return;
        prepage.updateData()
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    myCatchTouch: function() {
        return
    },

    getClassifyList: function() {
        const that = this
        let data = {
            store_id: wx.getStorageSync('storeId'),
            page: 0,
            limit: 999
        }
        common.post('Goodsclass/store_class_list', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                classifyList: res
            })
        }).catch(function(e) {})
    },

    topClassify: function(e) {

    },

    editClassify: function(e) {
        this.setData({
            editClassifyShow: true,
            editClassifyName: e.currentTarget.dataset.name,
            editClassifyId: e.currentTarget.dataset.id
        })
    },

    deleteClassify: function(e) {
        const that = this
        let data = {
            class_id: e.currentTarget.dataset.id
        }
        wx.showModal({
            title: '提示',
            content: '确认删除该分类？',
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '请稍后'
                    })
                    common.post('Goodsclass/store_del_class', data).then(function(res) {
                        wx.hideLoading()
                        if (res.code == 204) {
                            wx.showModal({
                                title: '提示',
                                content: res.error,
                                showCancel: false,
                                success(res) {}
                            })
                            return
                        }
                        wx.showModal({
                            title: '提示',
                            content: '成功删除分类',
                            showCancel: false,
                            success(res) {
                                if (res.confirm) {
                                    that.setData({
                                        editClassifyShow: false,
                                        editClassifyName: '',
                                        editClassifyId: '',
                                        classifyList: []
                                    })
                                    that.getClassifyList()
                                }
                            }
                        })
                    }).catch(function(e) {})
                }
            }
        })
    },

    classifyNameInput: function(e) {
        this.setData({
            editClassifyName: e.detail.value
        })
    },

    cancleClassifyShow: function() {
        this.setData({
            editClassifyShow: false,
            editClassifyName: ''
        })
    },

    sureEditClassify: function() {
        const that = this
        let data = {
            store_id: wx.getStorageSync('storeId'),
            class_name: this.data.editClassifyName,
            class_id: this.data.editClassifyId
        }
        common.post('Goodsclass/store_up_class', data).then(function(res) {
            if (res.code == 204) {
                wx.showModal({
                    title: '提示',
                    content: res.error,
                    showCancel: false,
                    success(res) {}
                })
                return
            }
            wx.showModal({
                title: '提示',
                content: '成功编辑分类',
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            editClassifyShow: false
                        })
                        that.getClassifyList()
                    }
                }
            })
        }).catch(function(e) {})
    }
})